package com.deram.ctin.serv.search.solr.service.impl;

import com.baomidou.mybatisplus.plugins.pagination.Pagination;
import com.deram.ctin.common.bean.PageResult;
import com.deram.ctin.serv.customs.entity.CustomsInfo;
import com.deram.ctin.serv.search.entity.solr.bean.SolrCustomsInfo;
import com.deram.ctin.serv.search.entity.solr.search.WebCustomsSearchParam;
import com.deram.ctin.serv.search.solr.reporsitory.SolrCustomsInfoDao;
import com.deram.ctin.serv.search.solr.service.SolrCustomsInfoService;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.solr.core.SolrTemplate;
import org.springframework.data.solr.core.query.Criteria;
import org.springframework.data.solr.core.query.SimpleQuery;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class SolrCustomsInfoServiceImpl implements SolrCustomsInfoService {

    @Autowired
    private SolrCustomsInfoDao solrCustomsInfoDao;

    @Autowired
    private SolrTemplate solrTemplate;

    @Override
    public SolrCustomsInfo save(SolrCustomsInfo solrCustomsInfo) {
        return solrCustomsInfoDao.save(solrCustomsInfo);
    }

    @Override
    public SolrCustomsInfo findById(Long id) {
        return solrCustomsInfoDao.findOne(id);
    }

    @Override
    public void delete(Long id) {
        solrCustomsInfoDao.delete(id);
    }

    @Override
    public PageResult<SolrCustomsInfo> findByKeyWords(WebCustomsSearchParam keywords, Integer pageNo, Integer pageSize) {
        Criteria criteria = createSearchConditions(keywords);
        Pageable pageable = new PageRequest(pageNo-1,pageSize);
        SimpleQuery search = new SimpleQuery(criteria);
        search.setPageRequest(pageable);
        Page reslut = solrTemplate.queryForPage( search,SolrCustomsInfo.class);
        PageResult<SolrCustomsInfo> pageResult = new PageResult<>();
        pageResult.setTotal((int) reslut.getTotalElements());
        pageResult.setList(reslut.getContent());
        pageResult.setPageNo(reslut.getNumber()+1);
        pageResult.setPageSize(pageSize);
        return pageResult;
    }

    @Override
    public List<SolrCustomsInfo> findAll() {
        Iterable<SolrCustomsInfo> iterable  =  solrCustomsInfoDao.findAll();
       List<SolrCustomsInfo> list =  Lists.newArrayList(iterable);
        return list;
    }

    @Override
    public List<SolrCustomsInfo> findByOne(CustomsInfo customsInfo) {
        List<SolrCustomsInfo> solrCustomsInfoList = (List<SolrCustomsInfo>) solrCustomsInfoDao.findAll();
        return solrCustomsInfoList;
    }

    @Override
    public List<SolrCustomsInfo> findByEnter(Long enterId) {
        return solrCustomsInfoDao.findByEnterId(enterId);
    }

//
//    @Override
//    public List<SolrCustomsInfo> searchByKeyWords(WebCustomsSearchParam  keywords) {
////        String[] words = keywords.split(" ");
//        Criteria criteria = createSearchConditions(words);
//        SimpleQuery search = new SimpleQuery(criteria);
//        Page reslut = solrTemplate.queryForPage( search,SolrCustomsInfo.class);
//        return reslut.getContent();
//    }


    /**
     * 动态封装所有海关查询的查询条件
     * @param words
     * @return
     */
    private Criteria createSearchConditions(WebCustomsSearchParam words) {
        Criteria conditions = null;

            if (conditions == null) {
//                String[] keywords = ToAnalysis.parse(words.getKeywords()).toString().split(",");
                conditions =new Criteria("dollarAmount").between(words.getDollarAmountLower(),words.getDollarAmountUpper()).and(new Criteria("enterId").is(words.getEnterId()))
                        .and(new Criteria("dollarPrice").between(words.getDollarPricePriceLower(),words.getDollarPricePriceUpper()))
                        .and(new Criteria("legalWeight").between(words.getLeaglWeightLower(),words.getLeaglWeightUpper()))
                        .or(new Criteria("exportDate").between(words.getStartDate(),words.getEndDate()))
                        .and(new Criteria("hsCode").contains(words.getHsCode()))
                        .and(new Criteria("hsName").contains(words.getHsName()))
                        .and(new Criteria("hsModels").contains(words.getHsModels()))
                        .and(new Criteria("exportCustoms").contains(words.getExportCustoms()))
                        .and(new Criteria("applicationUnit").contains(words.getApplicationUnit()))
                        .and(new Criteria("ownerUnit").contains(words.getOwnerUnit()))
                        .and(new Criteria("businessUnit").contains(words.getBusinessUnit()));
                if(words.getTradeModel()!=null){
                    conditions.and(new Criteria("tradeName").is(words.getTradeModel()));
                }
                if(words.getType()!=null){
                    conditions.and(new Criteria("type").is(words.getType()));
                }
            }
            else {
                conditions = new Criteria("keywords").contains(words.getKeywords())
                        .and(new Criteria("dollarAmount").between(words.getDollarAmountLower(),words.getDollarAmountUpper()))
                        .and(new Criteria("dollarPrice").between(words.getDollarPricePriceLower(),words.getDollarPricePriceUpper()))
                        .and(new Criteria("legalWeight").between(words.getLeaglWeightLower(),words.getLeaglWeightUpper()))
                        .or(new Criteria("exportDate").between(words.getStartDate(),words.getEndDate()))
                        .and(new Criteria("hsCode").contains(words.getHsCode()))
                        .and(new Criteria("hsName").contains(words.getHsName()))
                        .and(new Criteria("hsModels").contains(words.getHsModels()))
                        .and(new Criteria("exportCustoms").contains(words.getExportCustoms()))
                        .and(new Criteria("applicationUnit").contains(words.getApplicationUnit()))
                        .and(new Criteria("ownerUnit").contains(words.getOwnerUnit()))
                        .and(new Criteria("businessUnit").contains(words.getBusinessUnit()));
                if(words.getTradeModel()!=null){
                    conditions.and(new Criteria("tradeName").is(words.getTradeModel()));
                }
                if(words.getType()!=null){
                    conditions.and(new Criteria("type").is(words.getType()));
                }
            }
        return conditions;
    }
}